from flask import Blueprint, render_template, request, current_app
from models import House  # 根据你仓库 models 的位置调整导入路径
from app import db  # 如需

page = Blueprint('page', __name__, template_folder='templates')

@page.route('/hot')
def hot_list():
    """
    更多热点房源页，分页显示，按热度或浏览量排序。
    URL: /hot?page=1
    """
    # 分页参数
    page_num = request.args.get('page', 1, type=int)
    per_page = current_app.config.get('HOT_PER_PAGE', 20)

    # 优先按 `heat` 字段排序（若无则按 page_views）
    if hasattr(House, 'heat'):
        order_col = House.heat.desc()
    elif hasattr(House, 'page_views'):
        order_col = House.page_views.desc()
    else:
        order_col = House.id.desc()

    pagination = House.query.order_by(order_col).paginate(page=page_num, per_page=per_page, error_out=False)
    houses = pagination.items

    return render_template('hot.html', houses=houses, pagination=pagination)
